package com.itbaima.edu.info.manager.service;

import com.itbaima.edu.info.manager.dao.StudentDao;
import com.itbaima.edu.info.manager.domain.Student;

/**
 * Created by byakuya on 2020/8/5
 */
public class StudentService {  //业务员

    //创建库管对象
    private final StudentDao studentDao = new StudentDao();

    public boolean addStudent(Student stu) {

        return studentDao.addStudent(stu);
    }

    public boolean isExists(String id) {

        Student[] stus = studentDao.findAllStudent();
        //假设ID不存在
        boolean exists = false;
        //遍历数组，判断ID是否存在
        for (Student student : stus) {
            if (student != null && student.getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    public Student[] findAllStudent() {
        //调用库管对象方法获取学生数组
        Student[] allStudent = studentDao.findAllStudent();
        //判断数组中是否有学生信息，有，返回地址，否则返回null
        boolean flag = false;
        for (Student student : allStudent) {
            if (student != null) {
                return allStudent;
            }
        }
        return null;
    }

    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }

    public void updateStudent(String updateId, Student newStu) {
        studentDao.updateStudent(updateId, newStu);
    }
}
